package com.example.demo;

import lombok.Data;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.FileConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.Random;

@Component("myclass")
@Data
public class MyClass {

    @Value("${val}")
    private int val;

    @PostConstruct
    private void message() throws ConfigurationException {
        System.setProperty(net.sf.ehcache.CacheManager.ENABLE_SHUTDOWN_HOOK_PROPERTY,"true");
        PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration("application.properties");
        XMLConfiguration configuration = new XMLConfiguration("ehcache.xml");
        CacheManager cacheManager = CacheManager.create(configuration.getURL());
        Cache cache = cacheManager.getCache("mycache");
        List<String> list = cache.getKeys();
        for(String str : list){
            System.out.println(str);
        }
        for(int i=0; i<100; i++){
            cache.put(new Element(new Random().nextInt(),"a"));
        }
        cache.flush();
        cacheManager.shutdown();
    }


}
